package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReportQuery;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese01Economic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese03Econvalue;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class EconomicIndexValuesNotUpdatedFindQuery implements CustomExpressionFactory {

    /*SELECT * FROM economicIndex eIndex WHERE <month> - eIndex.updateFrequency >
    (
    SELECT MAX(eValue.effDate) FROM economicIndexValue eValue JOIN economicIndex eIndex 
        ON (eValue.economicIndex.pk = eIndex.pk)
    )*/
    
    public Expression createExpression(ExpressionBuilder builder, Object[] parameters) {        
        Teese01Economic economic = (Teese01Economic) parameters[0];
        Timestamp targetDate = (Timestamp) parameters[1];
        Expression exp = null;
        ReportQuery reportQuery = new ReportQuery(builder);
        reportQuery.setReferenceClass(Teese03Econvalue.class);
        reportQuery.addMaximum("eese03EcvalueEffectivDateY", builder.get("eese03EcvalueEffectivDateY"));

        exp = builder.get("eese03PlantCodeC").equal(economic.getEese01PlantCodeC());
        exp = exp.and(builder.get("eese03EcvalueTypeCodeC").equalsIgnoreCase("A"));
        exp = exp.and(builder.get("teese01Economic").equal(economic));
        //exp = exp.and(builder.get("eese03EcvalueEffectivDateY").greaterThan(targetDate));
        exp = exp.and(builder.get("eese03EcvalueEffectivDateY").lessThan(targetDate));
        exp = exp.and(builder.get("eese03EcvalueEffectivDateY").equal(builder.subQuery(reportQuery)));

        return exp;
    }

}
